package com.cskaoyan.order.biz.handler;

import com.cskaoyan.mall.commons.exception.ValidateException;
import com.cskaoyan.mall.commons.result.ServiceCallResponse;
import com.cskaoyan.mall.dto.ProductStatusDto;
import com.cskaoyan.order.biz.context.AbsTransHandlerContext;
import com.cskaoyan.order.biz.context.CreateOrderContext;
import com.cskaoyan.order.biz.context.TransHandlerContext;
import com.cskaoyan.order.remote.ProductApiClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
public class ValidateHandler extends AbstractTransHandler {

    @Autowired
    ProductApiClient productApiClient;


    @Override
    public boolean isAsync() {
        return false;
    }

    @Override
    public boolean handle(TransHandlerContext context) {
        // 校验参数是否正确
        CreateOrderContext createOrderContext = (CreateOrderContext) context;

        // 校验用户是否合法,是否存在，是否激活
        // 调用duolaimall-user的服务
        // TODO 暂时不做

        /*// 校验商品信息是否合法
        // 调用duolaimall-shoping的服务
        Object[] objects = ((CreateOrderContext) context).getCartProductDtoList().toArray();
        Long[] ids = (Long[]) objects;
        ServiceCallResponse<List<ProductStatusDto>> callProductStatus = productApiClient.getProductStatus(ids);
        List<ProductStatusDto> productStatus = callProductStatus.getCallData();

        // 如果有商品已下架或者商品已被删除条目，则抛出异常
        if (productStatus.size() != ids.length) {
            throw new ValidateException();
        }*/
        return true;
    }
}
